/******************************************************************************
 * The contents of this file are subject to the   JDI License  Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Initial Developer of the Original Code is 陈慧然. Portions created by 
 * 陈慧然 are Copyright 陈慧然.
 * All Rights Reserved.
 *
 *              Contact john_holen@163.com for licensing alternatives.
 *
 *****************************************************************************/

package org.peony.models;

import java.util.HashMap;
import java.util.Map;

import org.peony.db.base.PeonyDataSource;
import org.peony.exceptions.ConfigException;
import org.peony.standard.db.SAJDBCConnectionProviderFactory;
import org.peony.standard.db.SAJTAConnectionProviderFactory;
import org.peony.standard.unvarying.db.TranscationType;
import org.peony.tools.StringUtil;

/**
 * 
 * 
 * @author 陈慧然
 * @version Id: MDBConfig.java, v 0.000 2008-7-8 下午05:20:47 陈慧然 Exp
 */
public class MDBConfig {
	private static Map<String, PeonyDataSource> dataSource = new HashMap<String, PeonyDataSource>();
	private static String transactionType;

	public static void setModel(Map<String, PeonyDataSource> dataSource,
			String transactionType) {
		if (MDBConfig.dataSource.isEmpty()
				&& StringUtil.isEmpty(MDBConfig.transactionType)) {
			if (dataSource.isEmpty() || StringUtil.isEmpty(transactionType))
				throw new ConfigException(ConfigException.DBCONFIGERROR);
			if (transactionType.equals(TranscationType.JDBC)
					|| transactionType.equals(TranscationType.JTA)
					|| transactionType.equals(TranscationType.ALL)) {
				MDBConfig.dataSource = dataSource;
				MDBConfig.transactionType = transactionType;
			}
		} else
			throw new ConfigException(ConfigException.CANNOTRECONFIG);
	}

	public static String getTransactionType() {
		if (StringUtil.isEmpty(transactionType))
			throw new ConfigException(ConfigException.DBCONFIGISNOTAVIALIBLE);
		return transactionType;
	}

	public static PeonyDataSource getDataSource(String name) {
		if (dataSource == null || dataSource.isEmpty())
			throw new ConfigException(ConfigException.CANNOTRECONFIG);
		PeonyDataSource peonyDataSource = dataSource.get(name);
		if(peonyDataSource == null)
			throw new ConfigException(ConfigException.CANNOTRECONFIG);
		return peonyDataSource;
	}
}
